package com.alipay.service.impl;

import com.alipay.common.ServerResponse;
import com.alipay.dao.OrderMapper;
import com.alipay.entity.Order;
import com.alipay.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Random;

/**
 * Created by Yancy on 2019/11/9.
 */
@Service("iOrderService")
public class OrderServiceImpl implements IOrderService{

    @Autowired
    private OrderMapper orderMapper;

    public ServerResponse createOrder(Integer userId,String commodity, BigDecimal amount){
        Order order = new Order();
        Long orderNo = this.generateOrderNo();

        order.setUserId(userId);
        order.setCommodity(commodity);
        order.setAmount(amount);
        order.setOrderNo(orderNo);
        order.setStates(10);
        int rowCount= orderMapper.createOrder(order);
        if(rowCount == 0){
            return ServerResponse.createByErrorMessage("生成订单错误");
        }
        return ServerResponse.createBySuccess(order);
    }

    private long generateOrderNo(){
        long currentTime =System.currentTimeMillis();
        return currentTime+new Random().nextInt(100);
    }

    public ServerResponse pay(Integer id,Long orderNo){
        return null;
    }
}
